import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/Layout'
import Cookies from "js-cookie";

const routes = [

  //  ==== 登录 ====
  { path: '/login', name: 'Login', component: ()=> import('@/views/admin/Login')},

  {
    path: '/',
    name: 'Layout',
    component: Layout,
    //redirect实现重定向 从Layout定向到home
    redirect:"home",
    children:[
      //在children内部定义子路由

      //  ====主页路由====
      { path:'home', name:'Home', component:() => import("@/views/Home")},
      { path: '/person', name: 'Person', component: ()=> import('@/views/admin/Person')},

      //====  admin相关内容  ====

      //====  People  人员管理====
      // {path:'addPeople',name:'AddPeople',component:() => import('@/views/people/AddPeople')},
      { path:'userList',name:'UserList',component:() => import('@/views/people/user/UserList')},
      { path: 'editUser', name: 'EditUser', component: () => import('@/views/people/user/EditUser')},
      { path: 'addUser', name: 'AddUser', component: () => import('@/views/people/user/AddUser')},
      //  工作人员
      { path:'appointmentList',name:'AppointmentList',component:() => import('@/views/people/appointment/AppointmentList')},
      // { path: 'editStaff', name: 'EditStaff', component: () => import('@/views/people/staff/EditStaff')},
      // { path: 'addStaff', name: 'AddStaff', component: () => import('@/views/people/staff/AddStaff')},
      //  管理员
      { path:'adminList',name:'AdminList',component:() => import('@/views/admin/AdminList')},
      { path: 'editAdmin', name: 'EditAdmin', component: () => import('@/views/admin/EditAdmin')},
      { path: 'addAdmin', name: 'AddAdmin', component: () => import('@/views/admin/AddAdmin')},
      //====  Website  站点管理====
      //  轮播图 slides
      { path:'slidesList',name:'SlidesList',component:() => import('@/views/website/slides/SlidesList')},
      { path: 'editSlides', name: 'EditSlides', component: () => import('@/views/website/slides/EditSlides')},
      { path: 'addSlides', name: 'AddSlides', component: () => import('@/views/website/slides/AddSlides')},

      //  公告 notice
      { path:'noticeList',name:'NoticeList',component:() => import('@/views/website/notice/NoticeList')},
      { path: 'editNotice', name: 'EditNotice', component: () => import('@/views/website/notice/EditNotice')},
      { path: 'addNotice', name: 'AddNotice', component: () => import('@/views/website/notice/AddNotice')},

      //  展会 exhibition
      { path:'exhibitionList',name:'ExhibitionList',component:() => import('@/views/website/exhibition/ExhibitionList')},
      { path: 'editExhibition', name: 'EditExhibition', component: () => import('@/views/website/exhibition/EditExhibition')},
      { path: 'addExhibition', name: 'AddExhibition', component: () => import('@/views/website/exhibition/AddExhibition')},

      //====  collections  藏品管理====
      { path:'collectionsList',name:'CollectionsList',component:() => import('@/views/collections/CollectionsList')},
      { path: 'editCollections', name: 'EditCollections', component: () => import('@/views/collections/EditCollections')},
      { path: 'addCollections', name: 'AddCollections', component: () => import('@/views/collections/AddCollections')},

      //  collectionsType 藏品类别管理
      { path:'collectionsTypeList',name:'CollectionsTypeList',component:() => import('@/views/collectionstype/CollectionsTypeList')},
      { path: 'editCollectionsType', name: 'EditCollectionsType', component: () => import('@/views/collectionstype/EditCollectionsType')},
      { path: 'addCollectionsType', name: 'AddCollectionsType', component: () => import('@/views/collectionstype/AddCollectionsType')},

      //====  Mall  商城管理====

      //文创产品管理
      { path:'productsList',name:'ProductsList',component:() => import('@/views/mall/products/ProductsList')},
      { path: 'editProducts', name: 'EditProducts', component: () => import('@/views/mall/products/EditProducts')},
      { path: 'addProducts', name: 'AddProducts', component: () => import('@/views/mall/products/AddProducts')},

    //    文创产品类别管理
      { path:'productTypeList',name:'ProductTypeList',component:() => import('@/views/mall/producttype/ProductTypeList')},
      { path: 'editProductType', name: 'EditProductType', component: () => import('@/views/mall/producttype/EditProductType')},
      { path: 'addProductType', name: 'AddProductType', component: () => import('@/views/mall/producttype/AddProductType')},

    //    订单管理
      { path:'orderList',name:'OrderList',component:() => import('@/views/mall/order/OrderList')},
      { path: 'mallAnalyse', name: 'MallAnalyse', component: () => import('@/views/mall/order/MallAnalyse')},
    ]
  },
  //  主页所有页面都没有,提示404
  { path:'/:pathMatch(.*)', component:() => import('@/views/404')}
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//路由守卫,如果当前路径不是login,强制跳转到login
router.beforeEach((to,from,next)  => {
  if(to.path === '/login') next()
  const admin = Cookies.get("admin")
  if(!admin && to.path !== '/login')
    return next("/login")
  //访问 /home的时候,且cookie里存放数据,此时直接放行
  next()
})

router.afterEach((to, from, next) => {
  let title = "博物馆后台管理系统";
  document.title = title;
})



export default router
